package com.zhentao.controller;

import afu.org.checkerframework.checker.units.qual.A;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhentao.Exception.vo.ResultVo;
import com.zhentao.pojo.Address;
import com.zhentao.pojo.Category;
import com.zhentao.pojo.SysUser;
import com.zhentao.service.AddressService;
import com.zhentao.service.SysUserService;
import com.zhentao.service.UserService;
import com.zhentao.vo.ResultUser;
import com.zhentao.vo.RoleVO;
import com.zhentao.vo.UserInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.zhentao.service.FileStorageService;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * 用户管理控制器
 * 提供用户信息查询相关的REST接口
 *
 * @author zhentao
 * @since 2024-01-01
 */
@Api(tags = "用户管理接口", description = "提供用户信息查询、用户角色查询等功能")
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    /**
     * 用户服务接口
     * 通过构造器注入的方式注入UserService
     */
    private final UserService userService;

    /**
     * 获取用户详细信息
     *
     * @param id 用户ID
     * @return 返回包含用户基本信息和角色信息的对象
     * @throws RuntimeException 当用户不存在时抛出异常
     */
    @ApiOperation(value = "获取用户信息",
            notes = "根据用户ID获取用户详细信息，包括：\n" +
                    "1. 用户基本信息（ID、用户名、姓名、手机号等）\n" +
                    "2. 用户角色信息（角色列表）")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true,
            dataType = "Long", paramType = "path", example = "1")
    @GetMapping("/{id}")
    public ResultVo<UserInfoVO> getUserInfo(@PathVariable Long id) {
        return ResultVo.success(userService.getUserInfo(id));
    }
    //管理员查询所有用户
    @PostMapping("/findAllUser")
    public ResultUser FindAllUser(@RequestParam Long id) {
        return userService.findAllUser(id);
    }
    /**
     * 获取用户的角色列表
     *
     * @param userId 用户ID
     * @return 返回用户拥有的所有角色信息列表
     * @throws RuntimeException 当用户不存在时抛出异常
     */
    @ApiOperation(value = "获取用户角色",
            notes = "根据用户ID获取该用户拥有的所有角色信息，包括：\n" +
                    "1. 角色ID\n" +
                    "2. 角色名称\n" +
                    "3. 角色编码\n" +
                    "4. 角色描述")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true,
            dataType = "Long", paramType = "path", example = "1")
    @GetMapping("/{userId}/roles")
    public ResultVo<List<RoleVO>> getUserRoles(@PathVariable Long userId) {
        return ResultVo.success(userService.getUserRoles(userId));
    }
    @Autowired
    SysUserService sysUserService;
    @Autowired
    FileStorageService fileStorageService;
    @PostMapping("userUpdateShow")
    @SneakyThrows
    public String userUpdateShow(@RequestParam Integer id,@RequestParam("file") MultipartFile file){
        SysUser user = sysUserService.getById(id);
        String filename = file.getOriginalFilename();  // 获取原始文件名
        InputStream inputStream = file.getInputStream();  // 获取文件输入流
        // 调用文件存储服务上传图片，并获取访问URL
        String url = fileStorageService.uploadImgFile(filename, inputStream);
        user.setAvatar(url);
        boolean b = sysUserService.updateById(user);
        // 根据保存结果返回相应信息
        if (b) {
            return "修改成功";
        } else {
            return "修改失败";
        }
    }

    @Autowired
    AddressService addressService;
    @PostMapping("addressAdd")
    public Integer addressAdd(@RequestParam Integer userId,@RequestParam String name,@RequestParam String phone,@RequestParam String address){
        Address address1 = new Address();
        address1.setUserId(userId);
        address1.setName(name);
        address1.setPhone(phone);
        address1.setAddress(address);
        boolean save = addressService.save(address1);
        if(save){
            return 200;
        }else {
            return 401;
        }
    }

    @PostMapping("showAddress")
    public List<Address> showAddress(@RequestParam Integer userId){
        QueryWrapper<Address> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<Address> list = addressService.list(wrapper);
        return list;
    }
    @PostMapping("deleteAddress")
    public Integer deleteAddress(@RequestParam Integer id){
        boolean b = addressService.removeById(id);
        if(b){
            return 200;
        }else {
            return 401;
        }
    }
}
